package com.jingdianjichi.subject.application.controller;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.subject.application.convert.SubjectCategoryDTOConverter;
import com.jingdianjichi.subject.application.convert.SubjectLabelDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectCategoryDTO;
import com.jingdianjichi.subject.application.dto.SubjectLabelDTO;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.domain.convert.SubjectLabelConverter;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.service.SubjectCategoryDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("/subject/category")
@Slf4j
public class SubjectCategoryController {
    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("add subjectdto category", JSON.toJSONString(subjectCategoryDTO));
            }
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(),"分类类型不能为空");
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryName());
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId());
            SubjectCategoryBO subjectCategoryBO= SubjectCategoryDTOConverter.INSTANCE.convertDtoToBoCategory(subjectCategoryDTO);
            subjectCategoryDomainService.add(subjectCategoryBO);
            return Result.ok(true);
        } catch (Exception e) {
            return Result.fail("新增分类失败");
        }

    }
    @PostMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            List<SubjectCategoryBO> BoList=subjectCategoryDomainService.quiryCategory(
                    SubjectCategoryDTOConverter.INSTANCE.convertDtoToBoCategory(subjectCategoryDTO));
            List<SubjectCategoryDTO> DTOList= SubjectCategoryDTOConverter.INSTANCE.convertBoToDtoCategory(BoList);
            return Result.ok(DTOList);
        } catch (Exception e) {
            return Result.fail("查询失败");
        }

    }
    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {

            List<SubjectCategoryBO> subjectCategoryBOList=subjectCategoryDomainService.quiryCategory(
                    SubjectCategoryDTOConverter.INSTANCE.convertDtoToBoCategory(subjectCategoryDTO)
            );
            List<SubjectCategoryDTO> subjectCategoryDTOList= SubjectCategoryDTOConverter.INSTANCE.convertBoToDtoCategory(subjectCategoryBOList);
            return Result.ok(subjectCategoryDTOList);
        } catch (Exception e) {
            return Result.fail("查询失败");
        }

    }
    @PostMapping("/queryCategoryAndLabel")
    public Result<List<SubjectCategoryDTO>> queryCategoryAndLabel(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            if(log.isInfoEnabled()){
                log.info("queryCategoryAndPrimary subjectcategorydto", JSON.toJSONString(subjectCategoryDTO));
            }
            List<SubjectCategoryBO> subjectCategoryBOList=subjectCategoryDomainService.queryCategoryAndLabel(
                    SubjectCategoryDTOConverter.INSTANCE.convertDtoToBoCategory(subjectCategoryDTO)
            );
            List<SubjectCategoryDTO> subjectCategoryDTOList=new LinkedList<>();
            subjectCategoryBOList.forEach(item->{
               SubjectCategoryDTO scd=SubjectCategoryDTOConverter.INSTANCE.convertBoToDtoCategory(item);
               List<SubjectLabelDTO> subjectLabelDTOS = SubjectLabelDTOConverter.INSTANCE.convertBoToDtoLabel(item.getSubjectLabelList());
               scd.setLabelDTOList(subjectLabelDTOS);
               subjectCategoryDTOList.add(scd);
            });
            return Result.ok(subjectCategoryDTOList);
        } catch (Exception e) {
            return Result.fail("查询失败");
        }

    }

    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            boolean result=subjectCategoryDomainService.update(SubjectCategoryDTOConverter.INSTANCE.convertDtoToBoCategory(subjectCategoryDTO));
            return Result.ok(result);
        } catch (Exception e) {
            return Result.fail("更新失败");
        }

    }

    @PostMapping("/delete")
    public Result<Boolean> delete (@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        try {
            boolean result=subjectCategoryDomainService.delete(SubjectCategoryDTOConverter.INSTANCE.convertDtoToBoCategory(subjectCategoryDTO));
            return Result.ok(result);
        } catch (Exception e) {
            return Result.fail("删除失败");
        }

    }
}
